package com.ucap.bbs.controller.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.ucap.bbs.pojo.Reply;
import com.ucap.bbs.pojo.Users;
import com.ucap.bbs.service.IModeratorsService;
import com.ucap.bbs.service.IReplyService;
import com.ucap.bbs.service.ITopicService;
import com.ucap.bbs.util.ConstantUtil;
import com.ucap.bbs.util.GUID;
import com.ucap.bbs.util.StringUtils;
import com.ucap.bbs.util.bean.MessageInfoBean;

public class ReplyAction {
	private IReplyService replyService;
	private ITopicService topicService;
	private IModeratorsService moderatorsService;
	
	public void setTopicService(ITopicService topicService) {
		this.topicService = topicService;
	}

	public void setModeratorsService(IModeratorsService moderatorsService) {
		this.moderatorsService = moderatorsService;
	}

	public void setReplyService(IReplyService replyService) {
		this.replyService = replyService;
	}

	public String replyTopic() {
		Users loginUser = (Users)ActionContext.getContext().getSession().get(ConstantUtil.LOGIN_USER_BEAN);
		if(null!=loginUser){
			reply.setUserGuid(loginUser.getGuid());
		}else{
			messageInfoBean.addMsg("必须要登录才能回复！");
			messageInfoBean.setIsRedirect(true);
			messageInfoBean.setRedirect("login.jsp");
			return "msg";
		}
		
		if(StringUtils.isEmpty(reply.getContent())){
			messageInfoBean.addMsg("内容不能为空！");
			return "msg";
		}
		topicService.updateReplyNum(reply.getTopicGuid());
		reply.setGuid(GUID.getGUID());
		this.replyService.replyTopic(reply);
		
		messageInfoBean.addMsg("回复成功！");
		messageInfoBean.setIsRedirect(true);
		messageInfoBean.setRedirect("topic!queryTopicDetail.do?guid="+reply.getTopicGuid());
		return "msg";
	}
	
	private Reply reply;

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
	private MessageInfoBean messageInfoBean = new MessageInfoBean();
	
	public MessageInfoBean getMessageInfoBean() {
		return messageInfoBean;
	}

	public void setMessageInfoBean(MessageInfoBean messageInfoBean) {
		this.messageInfoBean = messageInfoBean;
	}
}
